package com.openspring.platform.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import javax.swing.SortOrder;
import org.springframework.stereotype.Service;
import com.openspring.framework.service.AbstractGenericService;
import com.openspring.framework.tree.Node;
import com.openspring.platform.dao.DeptDAO;
import com.openspring.platform.dao.RoleDAO;
import com.openspring.platform.model.Dept;
import com.openspring.platform.model.Role;
import com.openspring.platform.service.RoleService;

@Service("roleService")
public class RoleServiceImpl extends AbstractGenericService<Role, Long> implements RoleService {
	
	private RoleDAO roleDAO;
	private DeptDAO deptDAO;
	
	@Resource(name = "roleDAO")
	public void setRoleDAO(RoleDAO roleDAO) {
		super.setGenericDAO(roleDAO);
		this.roleDAO = roleDAO;
	}

	@Resource(name = "deptDAO")
	public void setDeptServie(DeptDAO deptDAO) {
		this.deptDAO = deptDAO;
	}	
	
	@Override
	public Node getUserRoleTree(Long id) {
		Node root = new Node();
		root.setText(".");
		Map<Long, Node> dmap = new HashMap<Long, Node>();
		Map<Long, List<Node>> umap = new HashMap<Long, List<Node>>();
		List<Dept> depts = deptDAO.queryAll();
		List<Map<String, Object>> users = roleDAO.queryUserRole(id);
		Iterator<Dept> diter = depts.iterator();
		Iterator<Map<String, Object>> uiter = users.iterator();
		//迭代部门列表以部门ID为key, 部门Node为value添加到部门HashMap
		while(diter.hasNext()) {
			Node dnode = new Node();
			Dept dept = diter.next();
			dnode.setExpanded(true);
			dnode.setText(dept.getCnName());
			dnode.setIconCls("dept");
			dnode.setParentId(dept.getParentId());
			dnode.setCustomId(dept.getId());
			dmap.put(dept.getId(), dnode);
		}
		//迭代用户列表以部门ID为key, 该部门下人员list为value添加到用户HashMap
		while(uiter.hasNext()) {
			Node unode = new Node();
			Map<String, Object> user = uiter.next();
			unode.setId(Long.parseLong(user.get("id").toString()));
			unode.setText(user.get("userName").toString());
			unode.setChecked(Boolean.valueOf(user.get("checked").toString()));
			unode.setIconCls("user");
			Long deptId = Long.parseLong(user.get("deptId").toString());
			List<Node> list = umap.get(deptId);
			if(list == null) {
				list = new ArrayList<Node>();
				list.add(unode);
			} else {
				list.add(unode);
			}
			umap.put(deptId, list);
		}
		Set<Map.Entry<Long, Node>> entrySet = dmap.entrySet();
		Iterator<Map.Entry<Long, Node>> iter = entrySet.iterator();
		while(iter.hasNext()) {
			//部门信息Node
			Node node = iter.next().getValue();
			List<Node> list = umap.get(node.getCustomId());
			if(list != null) {
				node.getChildren().addAll(list);
			}
			if(node.getParentId() == 0) {//根目录
				if(!node.getChildren().isEmpty())
					root.getChildren().add(node);
			} else {
				Node parentNode = dmap.get(node.getParentId());
				if(parentNode != null && !node.getChildren().isEmpty())
					parentNode.getChildren().add(node);
			}
		}
		root.sort(SortOrder.ASCENDING);
		return root;
	}
	
	@Override
	public void saveUserRole(Long id, Long...userId) {
		roleDAO.deleteUserRole(id);
		roleDAO.insertUserRole(id, userId);
	}
}
